﻿using RTW.DAL;
using RTW.OA.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RTW.OA.Areas.Personal.Servsers
{
    public class CalendarServer
    {
        private readonly IRepository<calendar> calendarRepository;
        public CalendarServer()
        {
            calendarRepository = new DapperRepository<calendar>();
        }
        public List<calendar> GetData()
        {
            var datas = calendarRepository.GetAllEnumerable();

            return datas.ToList();
        }

        public List<calendar> GetData(DateTime? CAL_TIME, DateTime? END_TIME, string CAL_TYPE, string CAL_LEVEL, string CONTENT, string OVER_STATUS)
        {
            var datas = calendarRepository.GetAllEnumerable();

            if (CAL_TIME != null)
                datas = datas.Where(m => m.CAL_TIME >= CAL_TIME);
            if (END_TIME != null)
                datas = datas.Where(m => END_TIME >= m.END_TIME);
            if (!string.IsNullOrWhiteSpace(CAL_TYPE))
                datas = datas.Where(m => m.CAL_TYPE == CAL_TYPE);
            if (!string.IsNullOrWhiteSpace(CAL_LEVEL))
                datas = datas.Where(m => m.CAL_LEVEL == CAL_LEVEL);
            if (!string.IsNullOrWhiteSpace(CONTENT))
                datas = datas.Where(m => m.CONTENT.Contains(CONTENT));
            if (!string.IsNullOrWhiteSpace(OVER_STATUS))
                datas = datas.Where(m => m.OVER_STATUS == OVER_STATUS);

            return datas.ToList();
        }

        internal void Add(calendar model)
        {
            calendarRepository.Insert(model);
        }
        internal void Update(calendar model)
        {
            calendarRepository.Update(model);
        }

        internal calendar GetModelById(string id)
        {
            return calendarRepository.GetById(id);
        }

        internal void Delete(string id)
        {
            calendarRepository.DeleteByIds(id);
        }

        internal List<SelectListItem> GetCalLevel_TYPE()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = calendarRepository.SqlWithParamsDynamic("select CODE_NO Value, concat(CODE_NO, '-----', CODE_NAME) Text  from[dbo].[sys_code] where PARENT_NO = 155");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }
            return tables;
        }

        internal List<SelectListItem> GetCalType_TYPE()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = calendarRepository.SqlWithParamsDynamic("select CODE_NO Value, concat(CODE_NO, '-----', CODE_NAME) Text  from[dbo].[sys_code] where PARENT_NO = 152");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }
            return tables;
        }

        internal List<SelectListItem> GetCalOverstatus_TYPE()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = calendarRepository.SqlWithParamsDynamic("select CODE_NO Value, concat(CODE_NO, '-----', CODE_NAME) Text  from[dbo].[sys_code] where PARENT_NO = 160");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }
            return tables;
        }

        internal List<SelectListItem> GetAllDay_TYPE()
        {
            List<SelectListItem> tables = new List<SelectListItem>();
            var dstable = calendarRepository.SqlWithParamsDynamic("select CODE_NO Value, concat(CODE_NO, '-----', CODE_NAME) Text  from[dbo].[sys_code] where PARENT_NO = 163");
            tables.Clear();

            foreach (var dr in dstable)
            {
                string Id = dr.Value.ToString();
                string Title = dr.Text.ToString().Trim();
                tables.Add(new SelectListItem { Value = Id, Text = Title });
            }
            return tables;
        }
    }
}